package com.footprint.hibernate.dao;

import com.footprint.common.core.dao.BaseHibernateDAO;
import com.footprint.hibernate.pojo.Ticket;

import java.sql.Timestamp;
import java.util.List;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.hibernate.LockMode;
import org.hibernate.Query;
import org.hibernate.criterion.Example;

/**
 * A data access object (DAO) providing persistence and search support for
 * Ticket entities. Transaction control of the save(), update() and delete()
 * operations can directly support Spring container-managed transactions or they
 * can be augmented to handle user-managed Spring transactions. Each of these
 * methods provides additional information for how to configure it for the
 * desired type of transaction control.
 * 
 * @see com.footprint.hibernate.pojo.Ticket
 * @author MyEclipse Persistence Tools
 */

public class TicketDAO extends BaseHibernateDAO {
	private static final Log log = LogFactory.getLog(TicketDAO.class);
	// property constants
	public static final String PRICE = "price";
	public static final String AIRCRAFT = "aircraft";
	public static final String AIRCRAFT_TYPE = "aircraftType";
	public static final String LAUNCH_TIME = "launchTime";
	public static final String ARRIVE_TIME = "arriveTime";
	public static final String YEAR = "year";
	public static final String MONTH = "month";
	public static final String DAY = "day";
	public static final String TICKET_COUT = "ticketCout";

	public void save(Ticket transientInstance) {
		log.debug("saving Ticket instance");
		try {
			getSession().save(transientInstance);
			log.debug("save successful");
		} catch (RuntimeException re) {
			log.error("save failed", re);
			throw re;
		}
	}

	public void delete(Ticket persistentInstance) {
		log.debug("deleting Ticket instance");
		try {
			getSession().delete(persistentInstance);
			log.debug("delete successful");
		} catch (RuntimeException re) {
			log.error("delete failed", re);
			throw re;
		}
	}

	public Ticket findById(java.lang.String id) {
		log.debug("getting Ticket instance with id: " + id);
		try {
			Ticket instance = (Ticket) getSession().get("com.footprint.hibernate.pojo.Ticket", id);
			return instance;
		} catch (RuntimeException re) {
			log.error("get failed", re);
			throw re;
		}
	}

	public List findByExample(Ticket instance) {
		log.debug("finding Ticket instance by example");
		try {
			List results = getSession().createCriteria("com.footprint.hibernate.pojo.Ticket").add(
					Example.create(instance)).list();
			log.debug("find by example successful, result size: " + results.size());
			return results;
		} catch (RuntimeException re) {
			log.error("find by example failed", re);
			throw re;
		}
	}

	public List findByProperty(String propertyName, Object value) {
		log.debug("finding Ticket instance with property: " + propertyName + ", value: " + value);
		try {
			String queryString = "from Ticket as model where model." + propertyName + "= ?";
			Query queryObject = getSession().createQuery(queryString);
			queryObject.setParameter(0, value);
			return queryObject.list();
		} catch (RuntimeException re) {
			log.error("find by property name failed", re);
			throw re;
		}
	}

	public List findByPrice(Object price) {
		return findByProperty(PRICE, price);
	}

	public List findByAircraft(Object aircraft) {
		return findByProperty(AIRCRAFT, aircraft);
	}

	public List findByAircraftType(Object aircraftType) {
		return findByProperty(AIRCRAFT_TYPE, aircraftType);
	}

	public List findByLaunchTime(Object launchTime) {
		return findByProperty(LAUNCH_TIME, launchTime);
	}

	public List findByArriveTime(Object arriveTime) {
		return findByProperty(ARRIVE_TIME, arriveTime);
	}

	public List findByYear(Object year) {
		return findByProperty(YEAR, year);
	}

	public List findByMonth(Object month) {
		return findByProperty(MONTH, month);
	}

	public List findByDay(Object day) {
		return findByProperty(DAY, day);
	}

	public List findByTicketCout(Object ticketCout) {
		return findByProperty(TICKET_COUT, ticketCout);
	}

	public List findAll() {
		log.debug("finding all Ticket instances");
		try {
			String queryString = "from Ticket";
			Query queryObject = getSession().createQuery(queryString);
			return queryObject.list();
		} catch (RuntimeException re) {
			log.error("find all failed", re);
			throw re;
		}
	}

	public Ticket merge(Ticket detachedInstance) {
		log.debug("merging Ticket instance");
		try {
			Ticket result = (Ticket) getSession().merge(detachedInstance);
			log.debug("merge successful");
			return result;
		} catch (RuntimeException re) {
			log.error("merge failed", re);
			throw re;
		}
	}

	public void attachDirty(Ticket instance) {
		log.debug("attaching dirty Ticket instance");
		try {
			getSession().saveOrUpdate(instance);
			log.debug("attach successful");
		} catch (RuntimeException re) {
			log.error("attach failed", re);
			throw re;
		}
	}

	public void attachClean(Ticket instance) {
		log.debug("attaching clean Ticket instance");
		try {
			getSession().lock(instance, LockMode.NONE);
			log.debug("attach successful");
		} catch (RuntimeException re) {
			log.error("attach failed", re);
			throw re;
		}
	}
}